import {format, Iterator, nextDate} from "../nextDate";
import {normalize} from "../normalize";
import {preNormalize} from "../pre_normize";
import {expect} from "chai";

describe('next date', () => {


  const nextFiveDates = (crontab: string, from: Date): string[] => {

    const preNormalized = preNormalize(crontab)
    const normalizeCrontab = normalize(preNormalized);

    let iter = new Iterator(normalizeCrontab, from)
    const nextDates = []
    for (let i = 0; i < 5; i++) {
      nextDates.push(iter.next())
    }

    return nextDates.map(d => format(d, true))
  }


  it('day || week 1', () => {
    const nextDates = nextFiveDates('0 0 13 * 2,3', new Date('2019-05-07 00:12:00'))

    expect(nextDates.join('\n'))
      .to.equal(
      `2019-05-08 00:00 (周三)
2019-05-13 00:00 (周一)
2019-05-14 00:00 (周二)
2019-05-15 00:00 (周三)
2019-05-21 00:00 (周二)`)

  })

  it('day && week 1', () => {
    const nextDates = nextFiveDates('0 0 13 * */4', new Date('2019-05-07 00:00:00'))

    expect(nextDates.join('\n'))
      .to.equal(
      `2019-06-13 00:00 (周四)
2019-10-13 00:00 (周日)
2020-02-13 00:00 (周四)
2020-08-13 00:00 (周四)
2020-09-13 00:00 (周日)`)
  })

  it('bug', () => {
    const preNormalized = preNormalize('1 2 1 12 */7')
    const normalizeCrontab = normalize(preNormalized);
    let iter = new Iterator(normalizeCrontab, new Date('2019-05-07 00:00:00'))

    expect(format(iter.next(), true)).to.equal('2019-12-01 02:01 (周日)')
  })

  it('bug date week or', () => {
    const preNormalized = preNormalize('1  2 3 12 1')
    const normalizeCrontab = normalize(preNormalized);
    let iter = new Iterator(normalizeCrontab, new Date('2019-05-07 00:00:00'))

    expect(format(iter.next(), true)).to.equal('2019-12-02 02:01 (周一)')
  })
  it('hour bug 3 * 1 4 1', () => {

    const dates = nextFiveDates('3 * 1 4 1', new Date('2019-05-18'))


    expect(dates).to.deep.equal(['2020-04-01 00:03 (周三)',
      '2020-04-01 01:03 (周三)',
      '2020-04-01 02:03 (周三)',
      '2020-04-01 03:03 (周三)',
      '2020-04-01 04:03 (周三)'])

  })
})
